package shuashuami.hb.com.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;
import org.w3c.dom.Text;

import shuashuami.hb.com.entity.agent.ApplyMsgBean;
import shuashuami.hb.com.entity.agent.ClickerBean;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/24<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/24<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class ClickViewApplyDialog extends Dialog{
    TextView parent_id;
    TextView dialog_content;

    TextView btn_refuse;
    TextView btn_agree;

    ClickerBean brush_id;

    Context mContext;


    public ClickViewApplyDialog(Context context) {
        super(context, R.style.Dialog);
        this.mContext=context;
        setCustomDialog();
    }

    private void setCustomDialog() {
        View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_clickapply_view, null);
        parent_id= (TextView) mView.findViewById(R.id.dialog_refer_id);
        dialog_content= (TextView) mView.findViewById(R.id.dialog_content);
        btn_refuse= (TextView) mView.findViewById(R.id.dialog_refuse);
        btn_agree= (TextView) mView.findViewById(R.id.dialog_agree);

        initListener();
        super.setContentView(mView);
    }

    private void initListener() {
        btn_agree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detalApply(true);
            }
        });

        btn_refuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detalApply(false);
            }
        });
    }

    public void detalApply(boolean isagree){
//        // 访问网络，查看url
        HttpOnNextListener<String> httpOnNextListener=new HttpOnNextListener<String>() {
            @Override
            public void onNext( String s) {

                Message msg=Message.obtain();
                msg.what=0x1;
                msg.obj=s;
                mHandler.sendMessage(msg);
            }
        };
//
//
        HttpAgentMethods.detalApplyInfo(httpOnNextListener,
                brush_id.getId(),isagree?1:0
        );
    }

    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what=msg.what;
            if(what==0x1){

                resolveByJson(msg.obj.toString());

          //      Toast.makeText(mContext,(String)msg.obj,Toast.LENGTH_LONG).show();

                ClickViewApplyDialog.this.dismiss();
            }
        }
    };

    public void resolveByJson(String str){
        try {
            JSONObject object=new JSONObject(str);
            int status=object.getInt("status");
            String message=object.getString("message");
            Toast.makeText(getContext(),message,Toast.LENGTH_LONG).show();

        }catch (Exception e){

        }

    }

    public void setContent(ClickerBean brush_id){
        this.brush_id=brush_id;
        parent_id.setText("推荐人ID："+brush_id.getMsgBean().getParent_id());
        dialog_content.setText(brush_id.getMsgBean().getMemo());
    }

    @Override
    public void setContentView(int layoutResID) {
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
    }

    @Override
    public void setContentView(View view) {
    }
}
